package com.zhny.project.txt.compute.controller;

import com.zhny.common.utils.poi.ExcelUtil;
import com.zhny.framework.aspectj.lang.annotation.Log;
import com.zhny.framework.aspectj.lang.enums.BusinessType;
import com.zhny.framework.web.controller.BaseController;
import com.zhny.framework.web.domain.AjaxResult;
import com.zhny.framework.web.page.TableDataInfo;
import com.zhny.project.txt.calculation.domain.TxtCalculation;
import com.zhny.project.txt.calculation.mapper.TxtCalculationMapper;
import com.zhny.project.txt.calculation.service.ITxtCalculationService;
import com.zhny.project.txt.compute.domain.Compute;
import com.zhny.project.txt.compute.mapper.ComputeMapper;
import com.zhny.project.txt.compute.service.IComputeService;
import com.zhny.project.txt.dataset.domain.TxtDataset;
import com.zhny.project.txt.dataset.mapper.TxtDatasetMapper;
import com.zhny.project.txt.gather.mapper.TxtGatherMapper;
import com.zhny.project.txt.model.domain.TxtModel;
import com.zhny.project.txt.model.mapper.TxtModelMapper;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 算法计算Controller
 *
 * @author yanqun
 * @date 2019-12-16
 */
@Controller
@RequestMapping("/txt/compute")
public class ComputeController extends BaseController
{
    private String prefix = "txt/compute";

    @Autowired
    private IComputeService computeService;
    @Autowired
    private TxtDatasetMapper txtDatasetMapper;

    @Autowired
    private ComputeMapper computeMapper;
    @Autowired
    private TxtGatherMapper txtGatherMapper;
    @Autowired
    private TxtModelMapper txtModelMapper;

    @Autowired
    private TxtCalculationMapper txtCalculationMapper;
    @Autowired
    private ITxtCalculationService txtCalculationService;



    @RequiresPermissions("txt:compute:view")
    @GetMapping()
    public String compute()
    {
        return prefix + "/compute";
    }

    /**
     * 查询算法计算列表
     */
    @RequiresPermissions("txt:compute:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(Compute compute)
    {
        startPage();
        List<Compute> list = computeService.selectComputeList(compute);
        return getDataTable(list);
    }

    /**
     * 导出算法计算列表
     */
    @RequiresPermissions("txt:compute:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(Compute compute)
    {
        List<Compute> list = computeService.selectComputeList(compute);
        ExcelUtil<Compute> util = new ExcelUtil<Compute>(Compute.class);
        return util.exportExcel(list, "compute");
    }

    /**
     * 新增算法计算
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存算法计算
     */
    @RequiresPermissions("txt:compute:add")
    @Log(title = "算法计算", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(Compute compute)
    {
        return toAjax(computeService.insertCompute(compute));
    }

    /**
     * 修改算法计算
     */
    @GetMapping("/edit/{datasetKey}")
    public String edit(@PathVariable("datasetKey") String datasetKey, ModelMap mmap)
    {
        Compute compute = computeService.selectComputeById(datasetKey);
        mmap.put("compute", compute);
        return prefix + "/edit";
    }

    /**
     * 修改保存算法计算
     */
    @RequiresPermissions("txt:compute:edit")
    @Log(title = "算法计算", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(Compute compute)
    {
        return toAjax(computeService.updateCompute(compute));
    }

    /**
     * 删除算法计算
     */
    @RequiresPermissions("txt:compute:remove")
    @Log(title = "算法计算", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(computeService.deleteComputeByIds(ids));
    }


    //成过模型 前端判断
    @GetMapping("/patterns/{datasetKey}")
    @ResponseBody
    public  Map<String, Object> patterns(@PathVariable("datasetKey") String datasetKey, ModelMap mmap)
    {

        Map<String,Object> map = new HashMap<>();
        int flag = 0;
        //查询表名
        TxtDataset txtDataset = txtDatasetMapper.selectTableNameByDatasetKey(datasetKey);
        //表名
        String tableName = "r_" + txtDataset.getSurfaceName();
        int datasetKeyCount = txtModelMapper.selectDatasetKeyCount(datasetKey);
        //txt_model 表中当前datasetKey数量 >0 该数据集已经生成过模型 否则该数据集没有生成过模型
        if (datasetKeyCount > 0){
            flag = 0;
        }else {
            flag = 1;
        }
        map.put("flag",flag);
        return  map;
    }

    //生成模型 执行算法及业务操作
    @GetMapping("/acope/{datasetKey}")
    public String acope( ModelMap mmap,@PathVariable("datasetKey") String datasetKey)
    {
        mmap.put("datasetKey",datasetKey);
        return prefix + "/createModal";
    }

//    @RequiresPermissions("deal:quotaset:edit")
    @Log(title = "指标集", businessType = BusinessType.UPDATE)
    @PostMapping("/createModal")
    @ResponseBody
    public AjaxResult editSaverange(TxtModel txtModel)
    {
        return toAjax(computeService.createModal(txtModel));
    }

    /**
     * 校验生成模型保存路径
     */

    @PostMapping("/checkSavePathUnique")
    @ResponseBody
    public String checkSavePathUnique(TxtModel txtModel)
    {
        return computeService.checkSavePathUnique(txtModel);
    }
    /**
     * 校验生成模型名称
     */

    @PostMapping("/checkModelNamehUnique")
    @ResponseBody
    public String checkModelNamehUnique(TxtModel txtModel)
    {
        return computeService.checkModelNamehUnique(txtModel);
    }

    @PostMapping("/checkCalculationNamehUnique")
    @ResponseBody
    public String checkCalculationNamehUnique(TxtCalculation txtCalculation)
    {
        return txtCalculationService.checkCalculationNamehUnique(txtCalculation);
    }



    //分析计算 前端判断
    @GetMapping("/calculation/{datasetKey}")
    @ResponseBody
    public  Map<String, Object> calculation(@PathVariable("datasetKey") String datasetKey) {
        Map<String, Object> map = new HashMap<String, Object>();
        int flag = 0;
        //txt_calculation 表中当前datasetKey 对应的calculationKeyCount >0 该数据集已经分析计算过 否则该数据集没有进行过分析计算
        int calculationKeyCount = txtCalculationMapper.selectTxtCalculationKeyCount(datasetKey);
        if (calculationKeyCount <= 0){
            //没有分析计算
            flag = 0;
        }else{
            //分析计算过
            flag = 1;
        }
        map.put("flag",flag);
        return  map;
    }

    @RequestMapping("/getModelName")
    @ResponseBody
    public Map<String,Object> getAlgorithm(String algorithmId){
        Map<String,Object> map = new HashMap<String,Object>();
       List<TxtModel> txtModelList = txtModelMapper.selectAlgorithmByModelKey(algorithmId);
       map.put("txtModelList",txtModelList);
        return  map;
    }


    //分析计算
    @GetMapping("/analyze/{datasetKey}")
    public String analyze( ModelMap mmap,@PathVariable("datasetKey") String datasetKey)
    {
        mmap.put("datasetKey",datasetKey);
        return prefix + "/editAnalyze";
    }

    @RequiresPermissions("txt:compute:edit")
    @Log(title = "指标集", businessType = BusinessType.UPDATE)
    @PostMapping("/editAnalyze")
    @ResponseBody
    public AjaxResult editAnalyze(TxtCalculation txtCalculation)
    {

        return toAjax(computeService.editAnalyze(txtCalculation));
    }

    @RequestMapping("/show")
    @ResponseBody
    public Map<String,Object> searchUse(String key){
        Map<String,Object> map = new HashMap<String,Object>();
        int flag = 0;
        int count = 0;
        //查询txt_indexset 表中指定datasetKey是否计算过特征值，dispose_icon = 4 代表付过特征值,做过特征工程
        TxtDataset infor = txtDatasetMapper.selectDisposeIcon(key);
        //txt_gather表中指定datasetKey的条数
        int datasetKeyCount = txtGatherMapper.selectDatasetKeyCount(key);
        if(("4").equals(infor.getDisposeIcon()) ){
            flag =  4;
        }else{
            flag = 1;
        }
        if(datasetKeyCount >= 1){
            //该数据集已经计算过特征值相关的指标集的最大值最小值
            count = 1;
        }else{
            //该数据集没有计算过特征值相关的指标集的最大值最小值
            count = 2;
        }
        map.put("flag",flag);
        map.put("count",count);
        return  map;
    }
}
